package main

import (
	"fmt"
)

/**
Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

实现strstr,找出字符串B在字符串A的位置并返回
*/

func strStr(haystack string, needle string) int {
	for i := 0; i <= len(haystack)-len(needle); i++ {
		tmp := haystack[i:]
		if hasPrefix(tmp, needle) {
			return i
		}
	}

	return -1
}

func hasPrefix(haystack, needle string) bool {
	if len(haystack) < len(needle) {
		return false
	}

	for i := 0; i < Min(len(haystack), len(needle)); i++ {
		if haystack[i] != needle[i] {
			return false
		}
	}

	return true
}

func Min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func main() {
	fmt.Print(strStr("hello", "ll"))
	//fmt.Print(strStr("mississippi", "issippi"))
}
